class CulturalHeritage::CulturalHeritagePreRegistersController < ApplicationController
  # GET /cultural_heritage/cultural_heritage_pre_registers
  # GET /cultural_heritage/cultural_heritage_pre_registers.xml
  def index
    @title_view = 'Pre Registro de Patrimonio Cultural'
    @cultural_heritage_cultural_heritage_pre_registers = CulturalHeritage::CulturalHeritagePreRegister.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_registers }
    end
  end

  # GET /cultural_heritage/cultural_heritage_pre_registers/1
  # GET /cultural_heritage/cultural_heritage_pre_registers/1.xml
  def show
    @title_view = 'Pre Registro de Patrimonio Cultural'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register }
    end
  end

  # GET /cultural_heritage/cultural_heritage_pre_registers/new
  # GET /cultural_heritage/cultural_heritage_pre_registers/new.xml
  def new
    @title_view = 'Nuevo Pre Registro de Patrimonio Cultural'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new
    @lista_instituciones = CulturalInstitution.all
     @cultural_heritage_cultural_heritage_pre_register.state = 5
     @cultural_heritage_cultural_heritage_pre_register.preRegisterRegistrationCode = PRE0 +@cultural_heritage_cultural_heritage_pre_register.id
#    @cultural_heritage_states = CulturalHeritage::State.all
    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
    @cultural_heritage_type_cultural_institutions = CulturalHeritage::TypeCulturalInstitution.all
    @cultural_heritage_periods = CulturalHeritage::Period.all
    @cultural_heritage_cultures = CulturalHeritage::Culture.all
    @cultural_heritage_styles = CulturalHeritage::Style.all
    @cultural_heritage_conservational_states = CulturalHeritage::ConservationalState.all
    @cultural_heritage_integrity_states = CulturalHeritage::IntegrityState.all
    @cultural_heritage_shapes = CulturalHeritage::Shape.all
    @cultural_heritage_techniques = CulturalHeritage::Technique.all
    @cultural_heritage_topics = CulturalHeritage::Topic.all

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register }
    end
  end

  # GET /cultural_heritage/cultural_heritage_pre_registers/1/edit
  def edit
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
  end

  # POST /cultural_heritage/cultural_heritage_pre_registers
  # POST /cultural_heritage/cultural_heritage_pre_registers.xml
  def create
     @title_view = 'Nuevo Pre Registro de Patrimonio Cultural'
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.new(params[:cultural_heritage_cultural_heritage_pre_register])
@cultural_heritage_cultural_heritage_pre_register.state = 5
     @cultural_heritage_cultural_heritage_pre_register.preRegisterRegistrationCode = PRE0 +@cultural_heritage_cultural_heritage_pre_register.id

    respond_to do |format|
      if @cultural_heritage_cultural_heritage_pre_register.save
        format.html { redirect_to(@cultural_heritage_cultural_heritage_pre_register, :notice => 'Pre Registro fue registrado correctamente.') }
        format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register, :status => :created, :location => @cultural_heritage_cultural_heritage_pre_register }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /cultural_heritage/cultural_heritage_pre_registers/1
  # PUT /cultural_heritage/cultural_heritage_pre_registers/1.xml
  def update
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])

    respond_to do |format|
      if @cultural_heritage_cultural_heritage_pre_register.update_attributes(params[:cultural_heritage_cultural_heritage_pre_register])
        if params[:pre_register_page].blank?
          format.html { redirect_to(@cultural_heritage_cultural_heritage_pre_register, :notice => 'Pre Registro fue actualizado correctamente.') }
        else
          format.html { redirect_to({:controller=>"cultural_heritage/pre_register_requests",:action=>"edit",:id=>params[:pre_register_page].to_i}) }
        end
        format.xml  { head :ok }
      else
        if params[:pre_register_page].blank?
          format.html { render :action => "edit" }
        else
          format.html { redirect_to({:controller=>"cultural_heritage/pre_register_requests",:action=>"edit",:id=>params[:pre_register_page].to_i}) }
        end
        
        format.xml  { render :xml => @cultural_heritage_cultural_heritage_pre_register.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /cultural_heritage/cultural_heritage_pre_registers/1
  # DELETE /cultural_heritage/cultural_heritage_pre_registers/1.xml
  def destroy
    @cultural_heritage_cultural_heritage_pre_register = CulturalHeritage::CulturalHeritagePreRegister.find(params[:id])
    @cultural_heritage_cultural_heritage_pre_register.deleted = 1
    @cultural_heritage_cultural_heritage_pre_register.save

    respond_to do |format|
      format.html { redirect_to(cultural_heritage_cultural_heritage_pre_registers_url) }
      format.xml  { head :ok }
    end
  end
end
